package com.project.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CutPagBean;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.dao.IRoomDao;
import com.project.dao.IRoomFacilityDao;
import com.project.dao.IStudentDao;
import com.project.mapper.IRoomFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class RoomServiceImpl implements IRoomService {


   @Autowired
   private IRoomMapper roomMapper;

    @Autowired
    private IStudentMapper studentMapper;
    @Autowired
    private IRoomFacilityMapper rfMapper;
    @Override
    public IPage<RoomBean> findAllRoom(int pageNO, String info, String state, String room, String type) {
        return roomMapper.findByItem(new Page(pageNO,3),room,
                state,info,type);
    }
    @Override
    public Boolean delRoom(Integer roomId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("fk_roomId",roomId);
        Integer stuNum = studentMapper.selectCount(qw);
        if(stuNum != 0){
            return false;
        }
        rfMapper.delete(qw);
        roomMapper.deleteById(roomId);
        return true;
    }

    @Override
    public RoomBean findByRooId(int id) {
        RoomBean room = roomMapper.findById(id);
        room.setStudentBeans(studentMapper.findByRoom(id));
        return room;
    }
    @Override
    public void addRoom(RoomBean roomBean) {
            roomMapper.insert(roomBean);
    }

    @Override
    public List<RoomBean> findByGender(String gender) {
        QueryWrapper<RoomBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("r_type",gender);
        List<RoomBean> list = roomMapper.selectList(queryWrapper);
        return list;
    }
}
